﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Compilex.BaseLibrary;

namespace Compilex.CodeGeneration
{
    internal class Labels
    {
        private static int tempResultsCount;
        private static int assignsCounts;
        static SimpleList<int> constants = new SimpleList<int>();
        private CodeGenerator Generator { get; set; }
        internal const string IndirectMoveStub = "INSTMOVE";
        internal const string IndirectLoadStub = "INSTLOAD";

        internal Labels(CodeGenerator generator)
        {
            this.Generator = generator;
        }

        internal string GetFunctionLabel(string name)
        {
            return "_F_" + name;
        }

        internal string GetAssignLabel()
        {
            assignsCounts++;
            return "_A_" + assignsCounts.ToString();
        }

        internal string GetConstantLabel(int constant)
        {
            if (!constants.Contains(constant))
            {
                Generator.GlobalDataBuilder.AppendLine("_C_" + constant.ToString() + " K =" + constant.ToString());
                constants.Add(constant);
            }
            return "_C_" + constant.ToString();
        }

        internal string IncrementResultsLabel()
        {
            tempResultsCount++;
            Generator.GlobalDataBuilder.AppendLine(GetResultLabel() + " K /0");
            return GetResultLabel();
        }

        internal string GetResultLabel()
        {
            return "_T_" + tempResultsCount;
        }

        internal string GetGlobalVariableLabel(string name)
        {
            return "_VAR_" + name;
        }

        internal string GetEpilogueLabel(string functionName)
        {
            return "_EP_" + functionName;
        }
    }
}
